package observer;

import java.util.ArrayList;

/**
 * 针对醒来这个事件做一个事件所有的信息都放在这里。
 */
class WakeupEvent<T>{
    private long timeStamp;
    private String loc;
    public T t;

    public void wakeupEvent(long timeStamp, String loc, T t){
        this.timeStamp = timeStamp;
        this.loc = loc;
        this.t = t;
    }

    @Override
    public String toString() {
        return "WakeupEvent{" +
                "timeStamp=" + timeStamp +
                ", loc='" + loc + '\'' +
                '}';
    }
}

interface ObserverIfn<T>{
    void actionWakeup(WakeupEvent<T> wakeupEvent);
}

class Child{
    private ArrayList<ObserverIfn> list = new ArrayList<>();

    public Child register(ObserverIfn observerIfn){
        list.add(observerIfn);
        return this;
    }

    public void say(){
        System.out.println("say");
    }

    public void wakeup(){
        WakeupEvent <Child>wakeupEvent = new WakeupEvent();
        wakeupEvent.wakeupEvent(
                System.currentTimeMillis(),
                "小孩醒了",
                this
        );
        for(ObserverIfn observerIfn:list){
            observerIfn.actionWakeup(wakeupEvent);
        }
        System.out.println("wuwu");
    }

}


class ObserveOne implements ObserverIfn<Child>{

    @Override
    public void actionWakeup(WakeupEvent<Child> wakeupEvent) {
        System.out.println(wakeupEvent);
        wakeupEvent.t.say();
    }
}



/**
 * Observer
 */
public class Observer {
    public static void main(String[] args) {
        Child child = new Child();
        child.register(new ObserveOne()).register(new ObserveOne()).wakeup();

    }
}
